package org.an.proj.contacts.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.Serializable;
import org.an.proj.contacts.MyApplication;
import org.an.proj.contacts.R;

/**
 * 创建时间：2017年02月27日17:26 <br>
 * 作者：fuchaoyang <br>
 * 描述：
 */

public class DialogActivity extends FragmentActivity implements View.OnClickListener {
  private static final String INFO = "info";
  @NonNull protected DialogActivityConfig mDialogInfo;

  private TextView mCancelBtn;
  private TextView mConfirmBtn;
  private TextView mContentTextView;
  private View mDivider;
  private ImageView mIvAlert;

  public static void startActivity(@NonNull DialogActivityConfig dialogInfo) {
    if (dialogInfo == null) return;
    // 由于这里是个Activity，所以只能通过bundle传递，必须要序列化，所以OnClickListener是个抽象类
    Intent intent = new Intent(MyApplication.getInstance(), DialogActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.putExtra(INFO, dialogInfo);
    MyApplication.getInstance().startActivity(intent);
  }

  @Override protected void onCreate(Bundle savedInstanceState) {
    setTheme(R.style.dialog_activity);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dialog_activity);
    init();
  }

  private void init() {
    mDialogInfo = (DialogActivityConfig) getIntent().getSerializableExtra(INFO);
    if (mDialogInfo == null) finish();

    // 点击activity空白处不消失也可以代码里面设置
    setFinishOnTouchOutside(false);
    mIvAlert = (ImageView) findViewById(R.id.iv_alert);
    mContentTextView = (TextView) findViewById(R.id.tv_content);
    mCancelBtn = (TextView) findViewById(R.id.btn_cancel);
    mConfirmBtn = (TextView) findViewById(R.id.btn_confirm);
    mDivider = findViewById(R.id.btn_divider);
    mCancelBtn.setOnClickListener(this);
    mConfirmBtn.setOnClickListener(this);
    mContentTextView.setText(mDialogInfo.content);

    if (mDialogInfo.drawableRes != 0) {
      mIvAlert.setImageResource(mDialogInfo.drawableRes);
    }

    if (!TextUtils.isEmpty(mDialogInfo.cancelBtnText)) {
      mCancelBtn.setText(mDialogInfo.cancelBtnText);
    } else {
      mCancelBtn.setVisibility(View.GONE);
    }
    if (!TextUtils.isEmpty(mDialogInfo.confirmBtnText)) {
      mConfirmBtn.setText(mDialogInfo.confirmBtnText);
    } else {
      mConfirmBtn.setVisibility(View.GONE);
    }

    if (TextUtils.isEmpty(mDialogInfo.cancelBtnText) || TextUtils.isEmpty(
        mDialogInfo.confirmBtnText)) {
      mDivider.setVisibility(View.GONE);
    }
  }

  @Override public boolean onKeyDown(int keyCode, KeyEvent event) {
    // 屏蔽返回键
    if (!mDialogInfo.closeable) {
      if (keyCode == KeyEvent.KEYCODE_BACK) return true;
    }
    return super.onKeyDown(keyCode, event);
  }

  @Override public void onClick(View v) {
    switch (v.getId()) {
      case R.id.btn_cancel:
        if (mDialogInfo.cancelListener != null) {
          mDialogInfo.cancelListener.onClick(v);
        } else {
          finish();
        }
        break;
      case R.id.btn_confirm:
        if (mDialogInfo.confirmListener != null) {
          mDialogInfo.confirmListener.onClick(v);
        }
        break;
    }
  }

  public static class DialogActivityConfig implements Serializable {
    @DrawableRes public int drawableRes;
    public String content;
    public boolean closeable;
    public String confirmBtnText;
    public OnClickListener confirmListener;
    public String cancelBtnText;
    public OnClickListener cancelListener;

    public DialogActivityConfig setIcon(int drawableRes) {
      this.drawableRes = drawableRes;
      return this;
    }

    public DialogActivityConfig setContent(String content) {
      this.content = content;
      return this;
    }

    public DialogActivityConfig setPositiveButton(String text, OnClickListener listener) {
      this.confirmBtnText = text;
      this.confirmListener = listener;
      return this;
    }

    public DialogActivityConfig setNegativeButton(String text, OnClickListener listener) {
      this.cancelBtnText = text;
      this.cancelListener = listener;
      return this;
    }

    public DialogActivityConfig setCloseable(boolean closeable) {
      this.closeable = closeable;
      return this;
    }

    public void show() {
      DialogActivity.startActivity(this);
    }
  }

  public abstract static class OnClickListener implements View.OnClickListener, Serializable {
  }
}
